package com.baic.ibase.service.mqtt.handler;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baic.ibase.service.devices.DeviceService;
import com.baic.ibase.service.mqtt.MqttMessageStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;

@Slf4j
@Component
public class DeviceDisconnectHandler implements MqttMessageStrategy {


    String disconnectedRegex = "^/ibase-device/([0-9]+)/([0-9]+)/disconnected$";
    Pattern pattern = Pattern.compile(disconnectedRegex);

    @Autowired
    DeviceService deviceService;

    @Override
    public boolean support(String topic) {
        return pattern.matcher(topic).matches();
    }

    @Override
    public void handle(JSONObject jsonObject) {
        deviceService.disconnected(jsonObject);
    }
}
